// 加载FORM表单扩展
layui.use(['form'], function(){
  var form = layui.form()
  ,layer = layui.layer;

	form.verify({
        apply_name: function(value){
			if(value.length < 1) {
				return '请填写应用名称';
			}
		  
		}
        ,user_age: function(value){
            var checkedTotal = 0;
            $("input[verify='user_age']").each(function(index, item){
                if( $(item).is(':checked') ) {
                    checkedTotal++;
                }
            });

            if( checkedTotal == 0 ) {
                return 0;
            }
        }
        ,dely_industry: function(value){
            var checkedTotal = 0;
            $("input[verify='dely_industry']").each(function(index, item){
                if( $(item).is(':checked') ) {
                    checkedTotal++;
                }
            });

        }
		,apply_classify: function(value){
			if(value.length < 1) {
				return '请选择应用分类';
			}
		}
		,apply_synopsis: function(value){
			if(value.length < 1) {
				return '请填写应用简介';
			}
		}
        ,android_ios_id: function(value){
            if(value.length < 1) {
                return '请填写应用ID';
            }
        }

	});

	//监听提交
	form.on('submit(submit)', function(data){
        var formData = data.field;
        var ageData    = Tool.getCheckBox(data.field, 'user_age');
        var ageLength  = ageData.length;

        var delyData=  Tool.getCheckBox(data.field, 'dely_industry');
        var  delyLength   =delyData.length;

        var access_mode          =   formData.access_mode;
        var user_age      =   ageData.join(',');
        var apply_name        =   formData.apply_name;
        var apply_platform       =   formData.apply_platform;
        var apply_classify           =   formData.apply_classify;
        var apply_synopsis        =   formData.apply_synopsis;
        var user_sex        =   formData.user_sex;
        var android_ios_id       =   formData.android_ios_id;
        var dely_industry           =   delyData.join(',');

        if(ageLength == 0) {
            user_age =0 ;
        }
        if(delyLength == 0) {
            dely_industry =0 ;
        }

	  $.ajax({
		  url: "/Apply/create",
		  type: "post",
		  data: {'access_mode':access_mode, 'user_age':user_age, 'apply_name':apply_name, 'apply_platform':apply_platform, 'apply_classify':apply_classify, 'apply_synopsis':apply_synopsis,
              'user_sex':user_sex, 'android_ios_id':android_ios_id, 'dely_industry':dely_industry},
		  dataType: "json",
		  success: function (msg) {
			if(msg.status == 'success') {
				  PopModel.refreshParentFrame();
			} else {
				layer.msg(msg.msg, {icon: 5, shift: 6}, function(){});
			}
		  }
	  });
	});
	//监听select
	form.on('select(media)', function(data){
		var media_id =data.value;
		$.ajax({
			url: "/Code/MediaTogroup",
			type: "post",
			data: {'id':media_id} ,
			dataType: "json",
			success: function (msg) {
			  if(msg.status == 'success') {

				  $("#group_id").empty();
				  $("#group_id").append(msg.codegroup);
				  form.render(); 
			  } else {
				  console.log('有错误了!');
			  }
			}
		});
    });
  
	// 刷新表单
	form.render();
});